package com.singzi.leetcode.N600;

import java.util.Arrays;

public class T0567 {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.checkInclusion("abc", "aacb"));
    }
}

class Solution {
    public boolean checkInclusion(String s1, String s2) {
        int n1 = s1.length(), n2 = s2.length();
        if(n1 > n2) return false;
        int []letters1 = new int[26];
        for (int i = 0; i < n1; i++) {
            letters1[s1.charAt(i) - 'a']++;
        }
        int []letters2 = new int[26];
        for(int i = 0;i < n1;i++){
            letters2[s2.charAt(i) - 'a']++;
        }
        if(Arrays.equals(letters1, letters2)) return true;
        for(int i = n1;i < n2;i++){
            letters2[s2.charAt(i-n1) - 'a']--;
            letters2[s2.charAt(i)-'a']++;
            if(Arrays.equals(letters1, letters2)) return true;
        }
        return  false;
    }
}